<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/1
 * Time: 16:38
 */

namespace app\api\home;

use app\api\validate\Userinfo;
use app\common\model\Userinfo as userinfoModel;
use app\common\model\User as userModel;
class User extends Base
{
    /**
     * 设置个人资料
     * @param string user_id 用户id
     * @param string sex 性别 0:女 1:男
     * @param string nickname 姓名
     * @param string face 头像
     * @return string
     * @throws \think\exception\DbException
     */
    public function setUserinfo()
    {
        //有效参数
        $value = app('request')->only(['user_id','nickname','face','phone']);

        $validate = new \app\api\validate\Userinfo();

        if(!$validate->scene('setuserinfo')->check($value)){
            return $this->ajaxError($validate->getError());
        }

        $data = [
            'user_id'=>$value['user_id'],
            'nickname'=>$value['nickname'],
            'face'=>$value['face'],
        ];

        $where = [
            'user_id'=>$value['user_id']
        ];

        $info = userinfoModel::where($where)->find();

        if(empty($info)){

            $userinfo = userinfoModel::create($data,['user_id']);

            return $this->ajaxReturn($userinfo,'设置成功');
        }

        $userinfoModel = new userinfoModel();

        $setinfo = $userinfoModel->save($data,$where);

        if($setinfo){
            //更改状态
            return $this->ajaxReturn('','更新成功');
        }

        return $this->ajaxError('未知错误');
    }

    /**
     * 提交认证资料
     * @param int user_id 用户ID
     * @param string name 用户名称
     * @param string idcard 身份证号
     * @param string idcards_back 身份证背面
     * @param string idcards_front 身份证正面
     * @param string license 营业执照
     * @return string
     * @throws \think\Exception
     */
    public function authentication()
    {
        //有效参数
        $value = app('request')->only(['user_id','name','idcard','idcards_back','idcards_front','license']);

        $validate = new Userinfo();

        if(!$validate->scene('authentication')->check($value)){
            return $this->ajaxReturn($validate->getError());
        }

        $userinfo = userinfoModel::where(['user_id'=>$value['user_id']])->find();

        $data = [
            'name'=>$value['name'],
            'idcard'=>$value['idcard'],
            'idcards_back'=>$value['idcards_back'],
            'idcards_front'=>$value['idcards_front'],
            'license'=>$value['license']
        ];

        $where = [
            'user_id'=>$value['user_id']
        ];

        if(empty($userinfo)){

            $data['user_id'] = $value['user_id'];

            $userinfo = userinfoModel::create($data);

            if(!$userinfo){
                return $this->ajaxError('未知错误');
            }

            userModel::status($value['user_id'],2);

            return $this->ajaxReturn('','认证提交成功');
        }

        $userinfo = userinfoModel::where($where)->update($data);

        if(!$userinfo){
            return $this->ajaxError('未知错误');
        }

        userModel::status($value['user_id'],2);

        return $this->ajaxReturn('','认证提交成功');
    }

}